package com.lk.wz.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.lk.wz.dao.UserDao;
import com.lk.wz.util.DBUtil;
import com.lk.wz.util.MD5Util;

public class UserDaoImpl implements UserDao{
	private final String ADD_USER="insert into forum_userinfo (user_name,user_password," 
								+"user_email) values(?,?,?)";
	private final String USER_NAME_COUNT="select count(*) from forum_userinfo where user_name=?";
	
	public void addUser(String userName,String password,String email) {
		Connection conn=DBUtil.getConnection();
		String final_password=MD5Util.getMD5(MD5Util.getMD5(password));
		try {
			PreparedStatement preStat=conn.prepareStatement(ADD_USER);
			preStat.setString(1, userName);
			preStat.setString(2, final_password);
			preStat.setString(3, email);
			preStat.executeUpdate();
			if(preStat!=null){
				preStat.close();
			}
			DBUtil.closeConnection(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public int validateUserName(String userName) {
		int count=0;
		Connection conn=DBUtil.getConnection();
		try {
			PreparedStatement preStat=conn.prepareStatement(USER_NAME_COUNT);
			preStat.setString(1, userName);
			ResultSet rs=preStat.executeQuery();
			rs.next();
			count=rs.getInt(1);
			System.out.println(count);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
}
